import { Validator } from "../Validator";

export interface ILengthAttribute {
    maxLength?: number;
    minLength?: number;
}

export class Length extends Validator {
    public validate(value: any, opt: any): boolean {
        const minLen = opt.minLength;
        const maxLen = opt.maxLength;
        if(minLen > 0) {
            if(value?.length < minLen) {
                this.error(`the value should be large than ${minLen}`, "minLength");
                return false;
            }
        }
        if(maxLen > 0) {
            if(value?.length > maxLen) {
                this.error(`the value should be less than ${maxLen}`, "maxLength");
                return false;
            }
        }
        return true;
    }
}
